package com.common.dongqiuhui.apps.comm.ui.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by fyh on 2014/9/18.
 */
public class BaseTextView extends TextView {
    public BaseTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BaseTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseTextView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        } catch (Exception e) {
            setText(getText().toString());
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    public void setGravity(int gravity) {
        try {
            super.setGravity(gravity);
        } catch (Exception e) {
            setText(getText().toString());
            super.setGravity(gravity);
        }
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        try {
            super.setText(text, type);
        } catch (Exception e) {
            super.setText(text.toString());
        }
    }

	private Runnable mDrawText = new Runnable() {
		@Override
		public void run() {
			try {
				setText(getText().toString());
			} catch (Throwable ex) {
				ex.printStackTrace();
			}
		}
	};

	@Override
	protected void onDraw(Canvas canvas) {
		//mi 1 绘制SpannedString时可能异常（StackOverflowError）
		try {
			super.onDraw(canvas);
		} catch (Throwable e) {
			e.printStackTrace();
			try {
				post(mDrawText);
			} catch (Throwable ex) {
				ex.printStackTrace();
			}
		}
	}
}
